package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐相关接口
 */
@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

    /**
     * 起售停售套餐
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @ApiOperation("起售停售套餐")
    @PostMapping("/status/{status}")
    public Result setMealStatus(@PathVariable Integer status,Long id){
        log.info("起售停售套餐:{} {}",status,id);
        setMealService.setMealStatus(status,id);
        return Result.success();
    }

    /**
     * 根据id查找套餐
     * @param id
     * @return
     */
    @ApiOperation("根据id查找套餐")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        log.info("根据id查找套餐:{}",id);
        SetmealVO setmealVO = setMealService.getById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     * @param dto
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @ApiOperation("修改套餐")
    @PutMapping
    public Result modifySet(@RequestBody SetmealDTO dto){
        log.info("修改套餐:{}",dto);
        setMealService.modifySet(dto);
        return Result.success();
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @ApiOperation("删除套餐")
    @DeleteMapping
    public Result deleteSet(@RequestParam List<Long> ids){
        log.info("删除套餐:{}",ids);
        setMealService.deleteSet(ids);
        return Result.success();
    }

    /**
     * 分页查询套餐
     * @param dto
     * @return
     */
    @ApiOperation("分页查询套餐")
    @GetMapping("/page")
    public Result pagesGetSet(SetmealPageQueryDTO dto){
        log.info("分页查询套餐:{}",dto);
        PageResult pageResult = setMealService.pagesGetSet(dto);
        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     * @param dto
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",key = "#dto.categoryId")
    @PostMapping
    @ApiOperation("新增套餐")
    public Result addSetMeal(@RequestBody SetmealDTO dto){
        log.info("新增套餐:{}",dto);
        setMealService.addSetMeal(dto);
        return Result.success();
    }
}
